import {
	lkrequest
} from './js/lk/request.js'

export function comjs(app) {


	app.config.globalProperties.wheight = uni.getSystemInfoSync().windowHeight
	// 获取图片地址
	app.config.globalProperties.IMGURL = "https://rh.wexiang.vip:11243/server/system/file/preview"

	// app.config.globalProperties.meetingip= 'api.51vmr.cn'
	// app.config.globalProperties.meetingip= '121.89.195.116'

	app.config.globalProperties.topay = (url) => {
		// #ifdef APP-PLUS
		uni.reLaunch({
			url: `/pages/web-view?url=` + url
		})
		// #endif
		// #ifdef H5
		location.href = url;
		// #endif
	}
	// app.config.globalProperties.topay = (url) => {
	// 	uni.reLaunch({
	// 		url: `/pages/web-view?url=` + url
	// 	})
	// }
	


	app.config.globalProperties.navto = (url) => {
		uni.navigateTo({
			url
		})
	}
	app.config.globalProperties.shh = uni.getSystemInfoSync().statusBarHeight

	app.config.globalProperties.istime = (time, newsp) => {
		if (time) {
			
			if (time * 1000 - new Date().getTime() > 0) {
				return true //没到
			} else {
				return false
			}
		}

	}

	app.config.globalProperties.timeTamp = (timestamp, type) => {

		if (typeof timestamp == 'number') {
			var date = new Date(timestamp * 1000);
		} else {
			var date = new Date(timestamp);
		}


		const year = date.getFullYear();
		const month = ('0' + (date.getMonth() + 1)).slice(-2);
		const day = ('0' + date.getDate()).slice(-2);

		const hours = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
		const minutes = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
		if (type == 'day') {
			return `${year}-${month}-${day} `;
		} else if (type == 'hours') {
			return `${hours}:${minutes}`;
		} else {
			return `${year}-${month}-${day} ${hours}:${minutes}`;
		}
	}


	app.config.globalProperties.copy = (copy) => {
		uni.setClipboardData({
			data: copy,
			success: function() {
			}
		})
	}
	

	app.config.globalProperties.back = () => {
		if (getCurrentPages().length == 1) {
			uni.reLaunch({
				url: "/pages/index/index"
			})
		} else {
			uni.navigateBack()
		}
	}



	if (uni.getStorageSync('token')) {
		let resra = uni.getStorageSync('user')
		window.loginMgr = new LoginMgr(resra.meetingIp, resra.port, {
			dev_id: resra.devid,
			dev_token: resra.devToken
		})

		let sre = resra.meetingUserName.split("@")
		loginMgr.login(sre[0], sre[1], resra.meetingUserPwd).then(res => {
			if (res.result == 0) {
				uni.setStorageSync('sdkinfo', res.data.results)
				loginMgr.put_self_status('1')
				
				loginMgr.on_login_failed = (data) => {
					uni.hideLoading()
					if (data.code == 5005) {
						// this.$refs.upLogOut.show()
						uni.$emit('onLogOut')
						uni.setStorageSync("token", '')
						uni.setStorageSync("user", '')
						uni.setStorageSync("info", '')
						loginMgr.put_self_status('0')
						
						// uni.reLaunch({
						// 	url: "/pages/index/login"
						// })
					}
				}
				
				loginMgr.on_receive_invite = (data) => {
					if(getCurrentPages()[0].route == 'pages/index/video'){//开会中不邀请
						return
					}
					// this.calldata = data
					const title = data.conference_name + '的私人会议';
					const body = '邀请您加入视频会议';
					const flag = JSON.stringify(data)
					const avatar = 'https://rh.wexiang.vip:11243/server/system/file/preview' + resra.user.avatar
					flutter.call('callWindow', {
						title,
						body,
						flag,
						avatar
					}, (result, error) => {
						if (error) {
							uni.showToast({
								title: error,
								icon: "none"
							})
							return;
						}
					})
				}
				
			}
		})

		setInterval(() => {
			lkrequest(`/user/refresh-token?token=${uni.getStorageSync('user').monitorToken}`, {}, 'post',
				'refresh-token')
		}, 1740000)

	}




}